package com.hh.vietnameseproject.ui.login

import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.logic.model.LoginData
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.MainActivity
import com.hh.vietnameseproject.ui.changeplandetails.PlanDetailsActivity
import com.hh.vietnameseproject.ui.forget.ForgetActivity
import com.hh.vietnameseproject.ui.register.RegisterActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.Response
import okhttp3.ResponseBody
import org.json.JSONObject
import org.w3c.dom.Text

class LoginPasswordTabFragment : Fragment() {

    val loginCodeUrl = "http://124.222.92.33/checkCode"

    lateinit var loginImagepwd: ImageView
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.login_password_fragment, container, false)
        loginImagepwd = view.findViewById<ImageView>(R.id.loginImagepwd)
        val loginBtn = view.findViewById<Button>(R.id.loginbtn)
        val loginPhone = view.findViewById<EditText>(R.id.loginPhone1)
        val loginPassword = view.findViewById<EditText>(R.id.loginpassword)
        val logincodepwd = view.findViewById<EditText>(R.id.logincodepwd)
        val loginShowText = view.findViewById<TextView>(R.id.loginshowTv)
        val loginCreate = view.findViewById<TextView>(R.id.loginCreatetv)
        val loginforget = view.findViewById<TextView>(R.id.loginforgetpasswordtv)


        loginBtn.setOnClickListener {
            val phoneNum = loginPhone.text.toString().trim()
            val pwd = loginPassword.text.toString().trim()
            val loginCode = logincodepwd.text.toString().trim()
            if (phoneNum == null || phoneNum.equals("")) {
                Toast.makeText(context, "请输入手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (pwd == null || pwd.equals("")) {
                Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (loginCode == null || loginCode.equals("")) {
                Toast.makeText(context, "请输入验证码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            GlobalScope.launch {
                val body = AllNetWork.loginPwd(phoneNum, pwd, loginCode)
                val jsonObject = JSONObject(body.string())
                val code = jsonObject.getInt("code")
                withContext(Dispatchers.Main) {
                    if (code == 1) {
                        Toast.makeText(context,"${jsonObject.getString("message")}",Toast.LENGTH_SHORT).show()
                        val dataObject = jsonObject.getJSONObject("data")
                        val loginData = LoginData(
                            dataObject.getInt("id"),
                            dataObject.getString("username"),
                            dataObject.getString("phone"),
                            dataObject.getInt("experience"),
                            dataObject.getInt("totaltime"),
                            dataObject.getInt("datecount"),
                            dataObject.getInt("thesaurusId")
                        )
                        val intent = Intent(context,MainActivity::class.java)
                        intent.putExtra("loginData",loginData)
                        startActivity(intent)
                        (context as LoginActivity).finish()
                    } else {
                        loginShowText.text = jsonObject.getString( "message")
                        updateCode()
                    }

                }
            }

        }
        loginImagepwd.setOnClickListener {
            updateCode()
        }

        loginCreate.setOnClickListener {
            val intent = Intent(context, RegisterActivity::class.java)
            context?.startActivity(
                intent,
                ActivityOptions.makeSceneTransitionAnimation(activity).toBundle()
            )
        }
        loginforget.setOnClickListener {
            val intent = Intent(context, ForgetActivity::class.java)
            context?.startActivity(
                intent,
                ActivityOptions.makeSceneTransitionAnimation(activity).toBundle()
            )
        }

        return view
    }

    fun updateCode() {
        GlobalScope.launch {
            val body = AllNetWork.getCheckCode()
            withContext(Dispatchers.Main) {
                loginImagepwd.setImageBitmap(BitmapFactory.decodeStream(body.byteStream()))
            }
        }
    }

    override fun onResume() {
        super.onResume()
        updateCode() //更新验证码
    }

}